<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['projectName']" placeholder="请输入项目名称"></a-input>
        </a-form-item>
        <a-form-item label="项目类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['projectType']" placeholder="请输入项目类型"></a-input>
        </a-form-item>
        <a-form-item label="项目来源" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['projectFrom']" placeholder="请输入项目来源"></a-input>
        </a-form-item>
        <a-form-item label="项目金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['projectAmount']" placeholder="请输入项目来源"></a-input>
        </a-form-item>
        <a-form-item label="项目简要说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['projectBrief']" rows="4" placeholder="请输入项目简要说明"/>
        </a-form-item>
        <a-form-item label="专家到场地点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['professorWorkAddress']" placeholder="请输入专家到场地点"></a-input>
        </a-form-item>

        <a-form-item label="专家类型和对应人数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <DynamicForm
            :data="childArray"
            v-decorator="['projectOwnProfessorType']" ref="DynamicForm"></DynamicForm>
        </a-form-item>

        <a-form-item label="专家到场时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date
            :showTime="true"
            dateFormat="YYYY-MM-DD HH:mm:ss"
            v-decorator="['professorWorkBeginTime']">
          </j-date>
        </a-form-item>
        <a-form-item label="项目预计评审时长" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['projectExpectTime']" placeholder="请输入预计评审时长"></a-input>
        </a-form-item>
        <a-form-item label="项目附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['projectFile']" :trigger-change="true"></j-upload>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import DynamicForm from '../modules/DynamicForm'
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JUpload from '@/components/jeecg/JUpload'
  import JCategorySelect from '@/components/jeecg/JCategorySelect'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: "ProjectProfessorInfoModal",
    components: { 
      JUpload,
      JCategorySelect,
      JDate,
      DynamicForm,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/projectProfessorInfo/add",
          edit: "/projectProfessorInfo/edit",
        },
        childArray:[],
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
        this.childArray=[];
      },
      edit (record) {
        console.log("record",record);
        this.form.resetFields();
        this.model = Object.assign({}, record);
        // console.log("model",this.model);
        // console.log("childtalbe",this.model.projectOwnProfessorType)
        // var pt=this.model.model.projectOwnProfessorType;
        // console.log("pt",pt);
        if(record!={}){
          this.childArray=this.model.projectOwnProfessorType;
        }
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'projectAmount','projectExpectTime','projectFrom','projectOwnProfessorType','projectName','projectType','professorType','professorNumber','projectBrief','projectFile','professorWorkBeginTime','professorWorkAddress'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        let childData = this.$refs.DynamicForm.data;
        this.form.setFieldsValue({
          projectOwnProfessorType: childData
        })
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'projectAmount','projectExpectTime','projectFrom','projectOwnProfessorType','projectName','projectType','professorType','professorNumber','projectBrief','projectFile','professorWorkBeginTime','professorWorkAddress'))
      },

      
    }
  }
</script>